package com.wt.struggle.goodbye.main.activity

import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.Message
import android.support.v7.widget.GridLayoutManager
import com.wt.struggle.goodbye.R
import com.wt.struggle.goodbye.base.ItemClickListener
import com.wt.struggle.goodbye.base.ProActivity
import com.wt.struggle.goodbye.info.FileInfo
import com.wt.struggle.goodbye.info.NetInfo
import com.wt.struggle.goodbye.main.adapter.PicAdapter
import com.wt.struggle.goodbye.view.CustomPop
import com.xin.lv.yang.utils.utils.FileUtils
import kotlinx.android.synthetic.main.head.*
import kotlinx.android.synthetic.main.send_content_layout.*

class SendActivity : ProActivity() {

    override fun handler(what: Int, arg1: Int, netInfo: NetInfo) {
        when (what) {

        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.send_content_layout)
        imageBack.setOnClickListener {
            finish()
        }
        tvTitle.text = "添加圈子"

        initAddPicAdapter()

        initClick()

    }

    private fun initClick() {
        videoLinearLayout.setOnClickListener {
            startActivity(Intent(this, AddVideoAndVoiceActivity::class.java))

        }

        btQuanSend.setOnClickListener {
            save()
        }
    }

    private fun save() {
        val name = etQuanName.text.toString()
        val theme = etQuanTheme.text.toString()
        val jianjie = etQuanJianJie.text.toString()
        when {
            name == "" -> showToastShort("请输入圈子名称")
            theme == "" -> showToastShort("请输入圈子主题")
            jianjie == "" -> showToastShort("请输入圈子简介")
            else -> {

            }
        }

    }

    lateinit var picAdapter: PicAdapter
    val picList = arrayListOf<FileInfo>()

    private fun initAddPicAdapter() {

        val fileInfo = FileInfo()
        fileInfo.text = R.drawable.pic_add.toString()
        picList.add(fileInfo)

        quanRecyclerView.layoutManager = GridLayoutManager(this, 3)
        picAdapter = PicAdapter(this, picList)
        picAdapter.code = 1

        quanRecyclerView.adapter = picAdapter

        picAdapter.itemClickListener = object : ItemClickListener {
            override fun onItemClick(position: Int) {
                if (position == picList.size - 1) {
                    /// 选择图片
                    choosePic()
                }

            }

            override fun onLongClick(position: Int) {

            }

        }
    }

    lateinit var customPop: CustomPop

    private fun choosePic() {
        customPop = CustomPop(this, 1)
        setAlpha(0.6f)
        customPop.show(object : CustomPop.OnCheckListener {
            override fun onCamera() {
                if (customPop.checkCameraPremission()) {
                    startActivityForResult(FileUtils.requestCamera(this@SendActivity), CustomPop.CAMERA_CODE)

                } else {
                    customPop.checkCamera(this@SendActivity)
                }

            }

            override fun onPhoto() {
                if (customPop.checkPhotoPremission()) {
                    startActivityForResult(FileUtils.openPhoto(), CustomPop.PHOTO_CODE)

                } else {
                    customPop.checkPhoto(this@SendActivity)
                }

            }

            override fun onClose() {
                setAlpha(1f)
                customPop.dismiss()
            }

        })
    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
        when (requestCode) {
            CustomPop.CAMERA_PERMISSION_CODE -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                    grantResults[1] == PackageManager.PERMISSION_GRANTED &&
                    grantResults[2] == PackageManager.PERMISSION_GRANTED
                ) {


                }

            }
            CustomPop.PHOTO_PERMISSION_CODE -> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED &&
                    grantResults[1] == PackageManager.PERMISSION_GRANTED
                ) {

                }

            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                CustomPop.CAMERA_CODE -> {
                }
                CustomPop.PHOTO_CODE -> {

                }
            }
        }
    }
}